﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Vara_Data_Layer.Vara_Data_Location
{
    public class Vara_Data_InternalLocation_Controller : Vara_Data_IAssetLocation_Actions
    {
        private static List<Vara_Data_IAssetLocation> _Locations;
        public List<Vara_Data_IAssetLocation> Locations
        {
            get
            {
                if (_Locations == null)
                    Refresh_Location_List();
                return _Locations;
            }
        }
        public void Refresh_Location_List()
        {
            DataTable DataTableLocations;
            using (SqlEngine NewSqlConnector = new SqlEngine("AssetTracking"))
            {
                DataTableLocations = NewSqlConnector.ExecuteProcedure("Return_Rooms");
            }
            List<Vara_Data_IAssetLocation> ListOfLocations = new List<Vara_Data_IAssetLocation>();
            foreach (DataRow R in DataTableLocations.Rows)
            {
                Vara_Data_InternalLocation LocationToAppend = new Vara_Data_InternalLocation(int.Parse(R["ID"].ToString()));
                LocationToAppend.Name = R["Name"].ToString();
                LocationToAppend.Description = R["Description"].ToString();
                LocationToAppend.Site = new Vara_Data_Site_Controller().ReturnSite(int.Parse(R["SiteId"].ToString()));
                Vara_Data_IAssetLocation LocationToAppendComplete = LocationToAppend;

                ListOfLocations.Add(LocationToAppendComplete);
            }
            _Locations = ListOfLocations;
        }
        public Vara_Data_IAssetLocation ReturnLocation(int ID)
        {
            Vara_Data_IAssetLocation LocationToReturn;
            LocationToReturn = Locations.Find(delegate(Vara_Data_IAssetLocation Location) { return Location.ID == ID; });
            return LocationToReturn;
        }
    }
}
